package com.yt.setclaim.server.api.cms;

import com.yt.common.constant.Result;
import com.yt.setclaim.models.entity.ClaimArchive;
import com.yt.setclaim.models.service.ClaimArchiveService;
import com.yt.setclaim.models.vo.ClaimArchiveVO;
import com.yt.setclaim.models.vo.ClaimCloseVO;
import com.yt.setclaim.models.dto.ClaimArchiveDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cms/setclaim-archive")
public class ClaimArchiveController {

    @Autowired
    private ClaimArchiveService claimArchiveService;

    /**
     * 获取所有理赔归档记录
     */
    @GetMapping("/list")
    public Result<List<ClaimArchiveVO>> list() {
        List<ClaimArchive> list = claimArchiveService.list();
        List<ClaimArchiveVO> voList = list.stream().map(claimArchive -> {
            ClaimArchiveVO vo = new ClaimArchiveVO();
            BeanUtils.copyProperties(claimArchive, vo);
            // ID转换为字符串类型
            vo.setArchiveId(String.valueOf(claimArchive.getArchiveId()));
            vo.setCloseId(String.valueOf(claimArchive.getCloseId()));
            vo.setAdminId(String.valueOf(claimArchive.getAdminId()));
            return vo;
        }).collect(Collectors.toList());
        return Result.success(voList);
    }
    
    /**
     * 获取待归档的理赔结案记录
     */
    @GetMapping("/pending-list")
    public Result<List<ClaimCloseVO>> getPendingCloses() {
        return Result.success(claimArchiveService.getPendingCloses());
    }

    /**
     * 根据ID获取理赔归档记录
     */
    @GetMapping("/info/{id}")
    public Result<ClaimArchiveVO> info(@PathVariable("id") Integer id) {
        ClaimArchive claimArchive = claimArchiveService.getById(id);
        ClaimArchiveVO vo = new ClaimArchiveVO();
        BeanUtils.copyProperties(claimArchive, vo);
        // ID转换为字符串类型
        vo.setArchiveId(String.valueOf(claimArchive.getArchiveId()));
        vo.setCloseId(String.valueOf(claimArchive.getCloseId()));
        vo.setAdminId(String.valueOf(claimArchive.getAdminId()));
        return Result.success(vo);
    }

    /**
     * 根据结案ID获取理赔归档记录
     */
    @GetMapping("/info-by-close/{closeId}")
    public Result<ClaimArchiveVO> infoByCloseId(@PathVariable("closeId") Integer closeId) {
        ClaimArchive claimArchive = claimArchiveService.getByCloseId(closeId);
        if (claimArchive == null) {
            return Result.fail("未找到对应的理赔归档记录");
        }
        ClaimArchiveVO vo = new ClaimArchiveVO();
        BeanUtils.copyProperties(claimArchive, vo);
        // ID转换为字符串类型
        vo.setArchiveId(String.valueOf(claimArchive.getArchiveId()));
        vo.setCloseId(String.valueOf(claimArchive.getCloseId()));
        vo.setAdminId(String.valueOf(claimArchive.getAdminId()));
        return Result.success(vo);
    }

    /**
     * 添加理赔归档记录
     */
    @PostMapping("/save")
    public Result<String> save(@RequestBody ClaimArchiveDTO claimArchiveDTO) {
        ClaimArchive claimArchive = new ClaimArchive();
        BeanUtils.copyProperties(claimArchiveDTO, claimArchive);
        // ID转换为Integer类型
        if (claimArchiveDTO.getArchiveId() != null) {
            claimArchive.setArchiveId(Integer.valueOf(claimArchiveDTO.getArchiveId()));
        }
        if (claimArchiveDTO.getCloseId() != null) {
            claimArchive.setCloseId(Integer.valueOf(claimArchiveDTO.getCloseId()));
        }
        if (claimArchiveDTO.getAdminId() != null) {
            claimArchive.setAdminId(Integer.valueOf(claimArchiveDTO.getAdminId()));
        }
        claimArchiveService.save(claimArchive);
        return Result.success("添加成功");
    }

    /**
     * 修改理赔归档记录
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody ClaimArchiveDTO claimArchiveDTO) {
        ClaimArchive claimArchive = new ClaimArchive();
        BeanUtils.copyProperties(claimArchiveDTO, claimArchive);
        // ID转换为Integer类型
        if (claimArchiveDTO.getArchiveId() != null) {
            claimArchive.setArchiveId(Integer.valueOf(claimArchiveDTO.getArchiveId()));
        }
        if (claimArchiveDTO.getCloseId() != null) {
            claimArchive.setCloseId(Integer.valueOf(claimArchiveDTO.getCloseId()));
        }
        if (claimArchiveDTO.getAdminId() != null) {
            claimArchive.setAdminId(Integer.valueOf(claimArchiveDTO.getAdminId()));
        }
        claimArchiveService.updateById(claimArchive);
        return Result.success("修改成功");
    }

    /**
     * 删除理赔归档记录
     */
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody ClaimArchiveDTO claimArchiveDTO) {
        claimArchiveService.removeById(Integer.valueOf(claimArchiveDTO.getArchiveId()));
        return Result.success("删除成功");
    }
}